/*
 * @Author: June
 * @Date: 2022-11-14 14:40:09
 * @LastEditors: June
 * @LastEditTime: 2023-05-22 17:08:56
 * @Description:
 *
 */
import { getInfo, login, logout } from '@/api/user'
import { resetRouter } from '@/router'
import { getCompInfo, getIsAdmin, getToken, removeCompInfo, removeIsAdmin, removeToken, setCompInfo, setIsAdmin, setToken } from '@/utils/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    isAdmin: getIsAdmin() || false,
    compInfo: getCompInfo() || {},
    fieldList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  IS_ADMIN: (state, type) => {
    state.isAdmin = type
    setIsAdmin(type)
  },
  SET_COMINFO: (state, compInfo) => {
    setCompInfo(compInfo)
    state.compInfo = compInfo
  },
  SET_FIELD: (state, fieldList) => {
    state.fieldList = fieldList
  }
}

const actions = {
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      removeToken()
      login(userInfo)
        .then((response) => {
          const { accessToken, adminType } = response.data.data
          console.log(accessToken)
          commit('SET_TOKEN', accessToken)
          commit('IS_ADMIN', adminType === 2)

          setToken(accessToken)

          commit('SET_COMINFO', response.data.data)
          // 清除模块点击缓存
          sessionStorage.removeItem('currentMenuData')
          setTimeout(() => {
            resolve(response.data.data)
          }, 0)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeCompInfo()
        removeIsAdmin()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  setField({ commit }, fieldList) {
    commit('SET_FIELD', fieldList)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

